import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author lys
 * 注册页面
 */

public class Register extends JFrame {
    private static Register instance;
    public JLayeredPane jLayeredPane = new JLayeredPane();
    public JPanel jPanel = new JPanel();


    //private JFrame jFrame = new JFrame();
    //标签
    private JLabel jLabel1 = new JLabel("注册");
    private JLabel jLabel2 = new JLabel(" 用户名 :");
    private JLabel jLabel3 = new JLabel("   姓名 :");
    private JLabel jLabel4 = new JLabel("   学号 :");
    private JLabel jLabel5 = new JLabel(" 新密码 :");
    private JLabel jLabel6 = new JLabel("确认密码:");

    //按钮
    private JButton jButton1 = new JButton("注册");
    private JButton jButton2 = new JButton("登录");

    //输入框
    private JTextArea jTextArea1 = new JTextArea(); //name
    private JTextArea jTextArea2 = new JTextArea(); //stuID
    private JTextArea jTextArea = new JTextArea();  //username
    private JPasswordField jPasswordField1 = new JPasswordField();
    private JPasswordField jPasswordField2 = new JPasswordField();

    //字体
    private Font font1 = new Font("宋体",Font.BOLD,60);
    private Font font2 = new Font("宋体",Font.BOLD,40);
    private Font font3 = new Font("宋体",Font.BOLD,30);

    private JPanel jPanel1 = new JPanel();
    private JPanel jPanel2 = new JPanel();
    private JPanel jPanel3 = new JPanel();
    private JPanel jPanel4 = new JPanel();
    private JPanel jPanel5 = new JPanel();

    private Dimension dimension = new Dimension(350, 50);


    private Register() {
        Icon i = new ImageIcon(Register.class.getResource("img/login2.jpg"));
        JLabel Label = new JLabel(i);
        Label.setBounds(0, 0, 800, 800);

        jLabel1.setFont(font1);
        jLabel1.setBounds(280, 50, 300, 80);
        jLabel1.setForeground(Color.white);

        //用户名
        jLabel2.setFont(font2);
        jLabel2.setBounds(150, 200, 200, 50);
        jLabel2.setForeground(Color.white);
        jTextArea.setPreferredSize(new Dimension(350,50));
        jTextArea.setBorder(BorderFactory.createLineBorder(Color.white,3));
        jTextArea.setFont(font2);
        jTextArea.setForeground(Color.black);
        jPanel1.setBounds(0,180,800,60);
        jPanel1.add(jLabel2);
        jPanel1.add(jTextArea);


        //姓名
        jLabel3.setFont(font2);
        jLabel3.setBounds(150, 270, 200, 50);
        jLabel3.setForeground(Color.white);
        //jTextArea1.setLayout(null);
        jTextArea1.setPreferredSize(new Dimension(350,50));
        jTextArea1.setBorder(BorderFactory.createLineBorder(Color.white,3));
        jTextArea1.setFont(font2);
        jTextArea1.setForeground(Color.black);
        jPanel2.setBounds(0,260,800,60);
        jPanel2.add(jLabel3);
        jPanel2.add(jTextArea1);

        //学号
        jLabel4.setFont(font2);
        jLabel4.setBounds(150, 360, 200, 60);
        jLabel4.setForeground(Color.white);
        // jTextArea2.setLayout(null);
        jTextArea2.setPreferredSize(new Dimension(350,50));
        jTextArea2.setBounds(350,340,350,50);
        jTextArea2.setBorder(BorderFactory.createLineBorder(Color.white,3));
        jTextArea2.setFont(font2);
        jTextArea2.setForeground(Color.black);
        jPanel3.setBounds(0,340,800,60);
        jPanel3.add(jLabel4);
        jPanel3.add(jTextArea2);

        //密码
        jLabel5.setFont(font2);
        jLabel5.setBounds(150, 440, 200, 50);
        jLabel5.setForeground(Color.white);
        //jPasswordField1.setLayout(null);
        jPasswordField1.setBounds(350,410,350,50);
        jPasswordField1.setPreferredSize(new Dimension(350,50));
        jPasswordField1.setBorder(BorderFactory.createLineBorder(Color.white,3));
        jPasswordField1.setFont(font2);
        jPasswordField1.setForeground(Color.black);
        jPanel4.setBounds(0,410,800,60);
        jPanel4.add(jLabel5);
        jPanel4.add(jPasswordField1);

        //确认密码
        jLabel6.setFont(font2);
        jLabel6.setBounds(150, 480, 200, 50);
        jLabel6.setForeground(Color.white);
        //jPasswordField2.setLayout(null);
        jPasswordField2.setBounds(350,480,350,50);
        jPasswordField2.setPreferredSize(new Dimension(350,50));
        jPasswordField2.setBorder(BorderFactory.createLineBorder(Color.white,3));
        jPasswordField2.setFont(font2);
        jPasswordField2.setForeground(Color.black);
        jPanel5.setBounds(0,480,800,60);
        jPanel5.add(jLabel6);
        jPanel5.add(jPasswordField2);


        jButton1.setBounds(280,600,300,60);
        jButton1.setFont(font2);
//        jButton1.setForeground(Color.white);
        jButton2.setBounds(620,650,100,50);
//        jButton2.setForeground(Color.white);
        jButton2.setFont(font3);

        jPanel1.setOpaque(false);
        jPanel2.setOpaque(false);
        jPanel3.setOpaque(false);
        jPanel4.setOpaque(false);
        jPanel5.setOpaque(false);

        jTextArea.setOpaque(false);
        jTextArea1.setOpaque(false);
        jTextArea2.setOpaque(false);
        jPasswordField1.setOpaque(false);
        jPasswordField2.setOpaque(false);


        add(jLabel1);
        add(jPanel1);
        add(jPanel2);
        add(jPanel3);
        add(jPanel4);
        add(jPanel5);
        add(jButton2);
        add(jButton1);
        add(Label);


        setVisible(true);
        setSize(800,800);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                submitRegister(jTextArea.getText(), jTextArea2.getText(), jTextArea1.getText(), jPasswordField1.getText(), jPasswordField2.getText());
            }
        });

        jButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                Login.getInstance().setVisible(true);
            }
        });

        jButton1.setFocusPainted(false);
        jButton2.setFocusPainted(false);
    }
    public static Register getInstance() {
        if(instance==null)  instance=new Register();
        return instance;
    }

    /**
     * @author xzy
     * @param userName
     * @return 用户名是否已重复
     * 以下方法类似
     */
    static boolean checkUsername(String userName){
        ResultSet rs = null;
        PreparedStatement preSql;
        String sqlStr;
        if(userName.equals(Admin.getInstance().getUsername())){
            return false;
        }
        Connection con = DatabaseConnection.getInstance().getCon();
        try{
            sqlStr = "select * from user where username = ?;";
            preSql = con.prepareStatement(sqlStr);
            preSql.setString(1, userName);
            rs = preSql.executeQuery();
            if(rs.next()){
                return false;
            }
            else
                return true;
        }
        catch(SQLException e){e.printStackTrace(); return false;}
    }

    static boolean checkSid(String sid){
        Pattern pattern = Pattern.compile("[0-9]{8}");
        return pattern.matcher(sid).matches();
    }

    static boolean checkName(String name){
        Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]{2,4}");
        return pattern.matcher(name).matches();
    }

    static boolean checkSame(String password1, String password2){
        if(password1.equals(password2)){
            return true;
        }
        return false;
    }

    static boolean checkPwd(String pwd){
        //不允许只包含数字或字符
        Pattern pattern1 = Pattern.compile("^(?![0-9]+$)(?![a-zA-Z]+$)[\\x21-\\x7E]{6,16}$");
        Matcher matcher1 = pattern1.matcher(pwd);
        //包含数字
        Pattern pattern2 = Pattern.compile("^.*[0-9].*$");
        Matcher matcher2 = pattern2.matcher(pwd);
        //包含字符
        Pattern pattern3 = Pattern.compile("^.*[a-zA-Z].*$");
        Matcher matcher3 = pattern3.matcher(pwd);
        return matcher1.find() && matcher2.find() && matcher3.find();
    }

    /**
     * 核对用户注册信息
     * @param username
     * @param sid
     * @param name
     * @param password1
     * @param password2
     */
    public void submitRegister(String username, String sid, String name, String password1, String password2){
        if(username.length() == 0 || sid.length() == 0 || name.length() == 0 || password1.length() == 0 || password2.length() == 0){
            JOptionPane.showMessageDialog(null, "请填写完整信息再注册！");
            return;
        }
        if(!checkUsername(username)){
            JOptionPane.showMessageDialog(null, "用户名已存在！");
            return;
        }
        if(!checkSid(sid)){
            JOptionPane.showMessageDialog(null, "请输入正确的学号！");
            return;
        }
        if(!checkName(name)){
            JOptionPane.showMessageDialog(null, "请输入正确的姓名！");
            return;
        }
        if(!checkSame(password1, password2)){
            JOptionPane.showMessageDialog(null, "两次密码不一致！");
            return;
        }
        if(!checkPwd(password1)){
            JOptionPane.showMessageDialog(null, "密码应由6-16位字母与数字混合组成");
            return;
        }
        String sql = "insert into user values (?, ?, ?, ?, 0)";
        Connection con = DatabaseConnection.getInstance().getCon();
        try{
            PreparedStatement preSql = con.prepareStatement(sql);
            preSql.setString(1, name);
            preSql.setString(2, sid);
            preSql.setString(3, username);
            preSql.setString(4, password1);
            int ok = preSql.executeUpdate();
            System.out.println(ok + " lines were influenced.");
        }catch (Exception e){
            System.out.println(e);
        }
        JOptionPane.showMessageDialog(null, "注册成功，请登录");
        Register.getInstance().setVisible(false);
        Login.getInstance().setVisible(true);
    }
}